package com.it.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.it.entity.coverage.AiInsuranceCoverage;
import com.it.mapper.AiInsuranceCoverageMapper;
import com.it.service.AiInsuranceCoverageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.tomcat.util.http.fileupload.FileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import utils.R;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 险种信息表 服务实现类
 * </p>
 *
 * @author lutao
 * @since 2025年09月17日
 */
@Service
public class AiInsuranceCoverageServiceImpl extends ServiceImpl<AiInsuranceCoverageMapper, AiInsuranceCoverage> implements AiInsuranceCoverageService {

    @Autowired
    private AiInsuranceCoverageMapper aiInsuranceCoverageMapper;
    @Override
    public R list(Integer pageNum, Integer pageSize, String coverageCode, String coverageName, Integer coverageType, Integer coverageStatus) {
        PageHelper.startPage(pageNum, pageSize);
        LambdaQueryWrapper<AiInsuranceCoverage> wrapper = new LambdaQueryWrapper<>();
//        wrapper.like(AiInsuranceCoverage::getCoverageCode,coverageCode)
//                .like(AiInsuranceCoverage::getCoverageName,coverageName)
//                .eq(AiInsuranceCoverage::getCoverageType,coverageType)
//                .eq(AiInsuranceCoverage::getCoverageStatus,coverageStatus);
        if (StringUtils.isNotBlank(coverageCode)){
            wrapper.like(AiInsuranceCoverage::getCoverageCode,coverageCode);
        }
        if (StringUtils.isNotBlank(coverageName)){
            wrapper.like(AiInsuranceCoverage::getCoverageName,coverageName);
        }
        if (coverageType != null){
            wrapper.eq(AiInsuranceCoverage::getCoverageType,coverageType);
        }
        if (coverageStatus != null){
            wrapper.eq(AiInsuranceCoverage::getCoverageStatus,coverageStatus);
        }
        List<AiInsuranceCoverage> list = aiInsuranceCoverageMapper.selectList(wrapper);
        PageInfo<AiInsuranceCoverage> pageInfo = new PageInfo<>(list);
        return R.Success("险种列表查询成功",pageInfo);
    }

    @Override
    public R listCoverageNames(Integer coverageType) {
        List<AiInsuranceCoverage> list = aiInsuranceCoverageMapper.selectList(new LambdaQueryWrapper<AiInsuranceCoverage>()
                .select(AiInsuranceCoverage::getCoverageName)
                .eq(AiInsuranceCoverage::getCoverageType,coverageType)
                .eq(AiInsuranceCoverage::getCoverageStatus,1));
        List<String> resList = list.stream().map(AiInsuranceCoverage::getCoverageName).collect(Collectors.toList());
        return R.Success("险种名称列表查询成功",resList);
    }

    @Override
    public R getUnitInsuredAmount(String coverageName) {
        AiInsuranceCoverage coverage = aiInsuranceCoverageMapper.selectOne(new LambdaQueryWrapper<AiInsuranceCoverage>()
                .select(AiInsuranceCoverage::getUnitInsuredAmount)
                .eq(AiInsuranceCoverage::getCoverageName,coverageName));
        return R.Success("险种单位保额查询成功",coverage.getUnitInsuredAmount());
    }
}
